package com.denso.workflow.admin.operation;

import com.denso.workflow.common.model.Operationlog;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OperationlogController extends Controller {

    static final OperationlogService service = new OperationlogService();

    public void index() {
        render("count.html");
    }

    public void count() {
        String dateStr = getPara("date");
        String[] strs = dateStr.split("-");
        int year = Integer.parseInt(strs[0]);
        int month = Integer.parseInt(strs[1]);
        int day = Integer.parseInt(strs[2]);

        List<Record> recordList = service.countByDay(year, month, day);
        //List<Record> recordList = createMockCount();

        setAttr("recordList", recordList);
        setAttr("date", dateStr);

        render("count.html");
    }

    private List<Record> createMockCount() {
        Record r1 = new Record();
        r1.set("operator", "张三");
        r1.set("ct", 102);

        Record r2 = new Record();
        r2.set("operator", "李四");
        r2.set("ct", 200);

        Record r3 = new Record();
        r3.set("operator", "王五");
        r3.set("ct", 48);

        Record r4 = new Record();
        r4.set("operator", "Tomas");
        r4.set("ct", 36);

        Record r5 = new Record();
        r5.set("operator", "王小虎");
        r5.set("ct", 102);

        List<Record> list = new ArrayList<Record>();
        list.add(r1);
        list.add(r2);
        list.add(r3);
        list.add(r4);
        list.add(r5);

        return list;
    }

    public void view() {
        String dateStr = getPara("date");
        String[] strs = dateStr.split("-");
        int year = Integer.parseInt(strs[0]);
        int month = Integer.parseInt(strs[1]);
        int day = Integer.parseInt(strs[2]);

        String operator = getPara("operator");

        List<Operationlog> operationlogList = service.viewByDay(year, month, day, operator);
        //List<Operationlog> operationlogList = createMockView();


        setAttr("operationlogList", operationlogList);
        setAttr("date", dateStr);
        setAttr("operator", operator);

        render("view.html");
    }

    public List<Operationlog> createMockView() {
        List<Operationlog> list = new ArrayList<Operationlog>();
        Operationlog operationlog = null;
        for(int i = 0; i < 100; i++) {
            operationlog  = new Operationlog();
            operationlog.setOperator("张三");
            operationlog.setBoxCode("TB014520-35810Z");
            operationlog.setPartCode("TB014520-35810Z");
            operationlog.setCreateTime(new Date());
            if(i % 2 == 0) {
                operationlog.setResult(0);
            } else {
                operationlog.setResult(-1);
            }
            list.add(operationlog);
        }
        return list;
    }


}
